Skip to content

Script to clean up branches#1218

Merged
aaronpowell merged 1 commit into
stagedfrom
branch-cleanup-script
Mar 30, 2026
Merged

Script to clean up branches#1218
aaronpowell merged 1 commit into
stagedfrom
branch-cleanup-script

Conversation

@aaronpowell
Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI review requested due to automatic review settings March 30, 2026 01:12
@aaronpowell aaronpowell changed the base branch from main to staged March 30, 2026 01:13
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ This PR targets main, but PRs should target staged.

The main branch is auto-published from staged and should not receive direct PRs.
Please close this PR and re-open it against the staged branch.

You can change the base branch using the Edit button at the top of this PR,
or run: gh pr edit 1218 --base staged

@aaronpowell aaronpowell merged commit 2794657 into staged Mar 30, 2026
10 checks passed
@aaronpowell aaronpowell deleted the branch-cleanup-script branch March 30, 2026 01:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a repository utility script to help developers clean up local git branches whose upstream remotes have been pruned (marked as [gone]).

Changes:

  • Introduces scripts/delete-gone-branches.sh to detect local branches with gone upstreams.
  • Supports dry-run by default and destructive deletion via --apply.
  • Skips deleting the currently checked-out branch.

Comment on lines +41 to +44
git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads |
while IFS= read -r line; do
branch=${line% *}
tracking=${line#"$branch "}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The for-each-ref output is parsed by splitting on the last space (branch=${line% *}), which is brittle and can mis-parse if the formatted fields ever contain spaces (or if the format is adjusted later). Prefer using an explicit delimiter in the --format (e.g., a tab) and read the two fields separately to make the selection of [gone] branches robust.

Suggested change
git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads |
while IFS= read -r line; do
branch=${line% *}
tracking=${line#"$branch "}
git for-each-ref --format=$'%(refname:short)\t%(upstream:track)' refs/heads |
while IFS=$'\t' read -r branch tracking; do

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +76
git branch -D "$branch"
deleted_count=$((deleted_count + 1))
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the script runs with set -e, any failure from git branch -D (e.g., branch checked out in another worktree, permissions, etc.) will terminate the whole script and skip deleting the remaining branches. Consider handling deletion per-branch (capturing failures and continuing) and only incrementing deleted_count on successful deletes, so one problematic branch doesn’t stop the cleanup run.

Suggested change
git branch -D "$branch"
deleted_count=$((deleted_count + 1))
if git branch -D "$branch"; then
deleted_count=$((deleted_count + 1))
else
echo "Failed to delete branch: $branch" >&2
fi

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants